<template>
	<view class="content">
		
		<web-view v-if="url" :src="url" @message="message" :key="webURLKey"></web-view>
		
	</view>
</template>

<script>
	import config from '@/common/scripts/config.js'
	import Util from '@/common/scripts/util.js'
	import Service from './service/service.js'
	import Storage from '@/common/scripts/storage.js'

	export default {
		beforeCreate() {
			this.service = new Service()
		},
		data() {
			return {
				url: null,
				code: null,
				userInfo: null,
				openID: null,
				unionID: null,
				encryptedData: null,
				iv: null,
				newCode: null,
				ticket: null,
				options: null,
				pianURL: null,
				webURLKey: 1
			}
		},
		created() {},
		onLoad(options) {
			let that = this
			that.options = options
			that.service.requestRegion({})
				.then(function (result){
					console.log(result)
					if (result.status == 1) {
						that.pianURL = result.url
						that.loadPageData(that.options)
					} else {
						that.loadTicketData(that.options)
					}
				})
				.catch(function (err) {
					that.loadPageData(that.options)
				})
		},
		mounted() {},
		methods: {
			loadPageData(options) {
				let that = this
				
				let backPage = Storage.has('back-page')
				
				let initPage = config.home
				
				if (options && options.hasOwnProperty('rt') && options.rt) {
					let path = '/' + options['lj_page'] + '?g=' + options['g'] + '&c=' + options['c'] + '&a=' + options['a'] + '&goods_id=' + options['goods_id']
					initPage = path
				}
				
				if (backPage) {
					Storage.get('back-page')
						.then(function(result) {
							console.log(result)
							if (result.errMsg == 'getStorage:ok' && result.data) {
								let backPageStr = result.data['lj_page'] + '?g=' + result.data['g'] + '&c=' + result.data['c'] + '&a=' + result.data['a']
								let pageData = result.data
								delete pageData['lj_page']
								delete pageData['g']
								delete pageData['c']
								delete pageData['a']
								if (!Util.isEmptyObj(pageData)) {
									for (let key in pageData) {
										backPageStr = backPageStr + '&' + key + '=' + pageData[key]
									}
								}
								if (that.pianURL) {
									that.url = that.pianURL
								} else {
									if (that.ticket) {
										if (options && options.hasOwnProperty('rt') && options.rt) {
											that.url = config.host + initPage + '&ticket=' + that.ticket + '&device_id=wxapp'
										} else {
											that.url = config.host + backPageStr + '&ticket=' + that.ticket + '&device_id=wxapp'
										}
									} else {
										if (options && options.hasOwnProperty('rt') && options.rt) {
											that.url = config.host + initPage
										} else {
											that.url = config.host + backPageStr
										}
									}
								}
								that.keyChange()
								console.log(that.url)
							} else {
								if (that.pianURL) {
									that.url = that.pianURL
								} else {
									if (that.ticket) {
										that.url = config.host + initPage + '&ticket=' + that.ticket + '&device_id=wxapp'
									} else {
										that.url = config.host + initPage
									}
								}
								console.log(that.url)
								that.keyChange()
							}
							Storage.remove('back-page')
						}).catch(function(err) {
							if (that.pianURL) {
								that.url = that.pianURL
							} else {
								if (that.ticket) {
									that.url = config.host + initPage + '&ticket=' + that.ticket + '&device_id=wxapp'
								} else {
									that.url = config.host + initPage
								}
							}
							that.keyChange()
							console.log(that.url)
						})
				} else {
					if (that.pianURL) {
						that.url = that.pianURL
					} else {
						if (that.ticket) {
							that.url = config.host + initPage + '&ticket=' + that.ticket + '&device_id=wxapp'
						} else {
							that.url = config.host + initPage
						}
					}
					that.keyChange()
					console.log(that.url)
				}
			},
			keyChange() {
				this.webURLKey = this.webURLKey > 1000 ? 1 : this.webURLKey + 1
			},
			loadTicketData(options) {
				let that = this
				let isHasTicket = Storage.has('ticket')
				if (isHasTicket) {
					Storage.get('ticket')
						.then(function(result) {
							console.log(result)
							if (result && result.data) {
								that.ticket = result.data
								that.loadPageData(options)
							} else {
								that.init()
							}
						})
						.catch(function(err) {
							console.log(err)
							that.init()
						})
				} else {
					uni.navigateTo({
						url: '/pages/index/login'
					})
				}
			},
			init() {
				let that = this
				this.service.login({})
					.then(that.loginSuccess).catch(that.loginError)
					.then(that.getUserInfoSuccess).catch(that.getUserInfoError)
					.then(that.getOpenIDSuccess).catch(that.getOpenIDError)
					.then(that.getCodeSuccess).catch(that.getCodeError)
					.then(that.getInfoSuccess).catch(that.getInfoError)
					.then(that.loadPage)
			},
			loginSuccess(data) {
				console.log(data)
				if (data) {
					this.code = data.code
					return this.service.getUserInfo()
				} else {
					return null
				}
			},
			loginError(err) {
				console.log(err)
				return null
			},
			getUserInfoSuccess(data) {
				let that = this
				console.log(data)
				
				if (data) {
					that.userInfo = data.userInfo
					that.iv = data.iv
					that.encryptedData = data.encryptedData
					return that.service.getOpenID({
						code: that.code
					})
				} else {
					return null
				}
			},
			getUserInfoError(err) {
				console.log(err)
				return null
			},
			onShareAppMessage(e) {
				
			},
			getOpenIDSuccess(data) {
				let that = this
				console.log(data)
				if (data) {
					this.openID = data.openid
					this.unionID = data.unionid
					console.log(data)
					return this.service.login({})
				} else {
					return null
				}
			},
			getOpenIDError(err) {
				console.log(err)
				return null
			},
			getCodeSuccess(data) {
				let that = this
				console.log(data)
				if (data) {
					this.newCode = data.code
					let options = {
						code: that.newCode,
						encryptedData: that.encryptedData,
						iv: that.iv
					}
					console.log(options)
					return this.service.getInfo(options)
				} else {
					return null
				}
			},
			getCodeError(err) {
				console.log(err)
				return null
			},
			getInfoSuccess(data) {
				console.log(data)
				let that = this
				if (data && data.errorCode == 0) {
					let isHasTicket = Storage.has('ticket')
					let ticketStr = ''
					let isHasUser = Storage.has('user')
					that.ticket = data.result.ticket
					if (!isHasUser) {
						Storage.set('user', data.result.user)
					}
					if (!isHasTicket) {
						return Storage.set('ticket', data.result.ticket)
					} else {
						return null
					}
				} else {
					return null
				}
			},
			getInfoError(err) {
				console.log(err)
				return null
			},
			loadPage(result) {
				this.loadPageData(this.options)
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.tabbar {
		position: fixed;
		bottom: 0px;
		width: 100%;
	}
</style>
